package boot.spring.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import boot.spring.pagemodel.DataGrid;
import boot.spring.po.Picture;
import boot.spring.service.PictureService;

@Controller
@RequestMapping("/mongodb")
public class MongodbController
{
    @Resource(name = "mongodbServiceImpl")
    private PictureService mongodbService;
    
    @ResponseBody
    @GetMapping("/pictures")
    public DataGrid<Picture> getpiclist(@RequestParam("current") int current, @RequestParam("rowCount") int rowCount, @RequestParam(required = false, value = "sort[id]") String sortid,
        @RequestParam(required = false, value = "searchPhrase") String searchPhrase)
    {
        if (searchPhrase == null)
        {
            List<Picture> list = mongodbService.getpiclist(current, rowCount, sortid);
            int total = mongodbService.getpicturenum();
            DataGrid<Picture> grid = new DataGrid<Picture>();
            grid.setCurrent(current);
            grid.setRowCount(rowCount);
            grid.setRows(list);
            grid.setTotal(total);
            return grid;
        }
        List<Picture> list = mongodbService.getsearchresult(current, rowCount, sortid, searchPhrase);
        int total = mongodbService.getsearchresulttotal(searchPhrase);
        DataGrid<Picture> grid = new DataGrid<Picture>();
        grid.setCurrent(current);
        grid.setRowCount(rowCount);
        grid.setRows(list);
        grid.setTotal(total);
        return grid;
    }
    
    @GetMapping("/index")
    public String index()
    {
        return "mongodb";
    }
    
    @ResponseBody
    @DeleteMapping("picture/{id}")
    public void deletepicture(@PathVariable("id") String id)
    {
        mongodbService.deletePicture(id);
    }
    
    @ResponseBody
    @GetMapping("/picture/{id}")
    public Picture getpicture(@PathVariable("id") String id)
    {
        return mongodbService.getPictureByid(id);
    }
    
    // 这个方法被新增和修改请求共用,如果是新增，id必须为空，否则是修改请求
    @PostMapping("/picture")
    public String addpicture(@ModelAttribute("picture") Picture picture)
    {
        mongodbService.SaveorUpdatePicture(picture);
        return "mongodb";
    }
    
    // 创造的测试数据
    @ResponseBody
    @GetMapping("add")
    public void add()
    {
        for (int i = 0; i < 10000; i++)
        {
            Picture p = new Picture("w", "java", (long)i);
            mongodbService.SaveorUpdatePicture(p);
        }
    }
}
